import axios from 'axios'
import { Message } from 'element-ui'
import { getToken } from '@/utils/auth'
import {SYSTEM_ERROR} from './common'
// create an axios instance
const service = axios.create({
  headers: {'content-type': 'application/json'},
  baseURL: process.env.BASE_API, // api的base_url
  timeout: 60000 // request timeout
})

// request interceptor
service.interceptors.request.use(config => {

    //if(config.method === 'post') {
      config.headers['Authorization'] = "Bearer "+ getToken();
      //config.url +=`?access_token=`+getToken();
    /*} else if(config.method === 'get') {
      config.params = {
        access_token: getToken(),
        ...config.params
      }
    }*/
    return config;
  },

  err => {
    return Promise.reject(err);
  }
);

// respone interceptor
service.interceptors.response.use(
  response =>{
  /* var status = response.status;
    var result =response.data;
   if(status !== 200){
       Message({
         message: result.msg,
         type: 'error',
         duration: 2 * 1000
       })
   }*/
    return Promise.resolve(response);
  },

  error => {

    Message({
      message: error.response.data.msg?error.response.data.msg: SYSTEM_ERROR,
      type: 'error',
      duration: 2 * 1000
    })
    return Promise.reject(error)
  })

export default service
